package com.cheetah.start.common.utils;

import cn.hutool.core.util.DesensitizedUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 日志敏感数据脱敏
 *
 * @author Administrator
 * @date 2022/11/29
 */
public class MyDesensitizedUtil {

    /**
     * 简单脱敏
     *
     * @param content 内容 password=[1234]
     * @param keys    键 password
     * @return {@link String} password=[****]
     */
    public static String desensitized(String content, String... keys) {
        for (String key : keys) {
            String reg = "(?<=" + key + "(=\\[)|(\":\")|(=)).*?(?=(\\])|(\\\")|(,))";
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(content);
            if (matcher.find()) {
                String group = matcher.group();
                content = matcher.replaceAll(DesensitizedUtil.password(group));
            }
        }
        return content;
    }
}
